package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.param.UserLocationParam;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.service.StoreChainService;
import com.tarena.lbs.business.service.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Tag(name = "店铺相关功能接口")
@RestController
public class StoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreChainService storeChainService;
   @Operation(summary = "查询店铺列表")
@PostMapping("/admin/basic/store/getAll")
public Result<PageResult<StoreVO>> pageList(@RequestBody StoreQuery query) {
    log.debug("开始查询店铺列表，入参：{}", query);

    // 获取当前登录用户的商家ID（这里需要根据实际的用户认证系统获取）
    // 假设从ThreadLocal或SecurityContext中获取当前登录用户信息
    Integer businessId = getCurrentLoginUserBusinessId();
    if (businessId != null) {
        query.setBusinessId(businessId);
        log.debug("设置当前登录用户的商家ID: {}", businessId);
    }

    PageResult<StoreVO> pageResult = storeService.pageList(query);
    return new Result<>(pageResult);
}

// 获取当前登录用户的商家ID的辅助方法
private Integer getCurrentLoginUserBusinessId() {
    // 根据实际的用户认证系统实现
    // 示例：return UserContext.getCurrentUser().getBusinessId();
    return null; // 实际项目中替换为真实实现
}


    /**
     * 新增店铺
     * @param param
     * @return
     * @throws BusinessException
     */
    @Operation(summary = "新增店铺")
    @PostMapping("/admin/basic/store/add")
    public Result<Void> saves(@RequestBody StoreParam param)
        throws BusinessException {
        log.info("接收到新增店铺请求，请求参数：{}", param);
        log.debug("开始保存店铺数据，入参：{}", param);
        storeService.saves(param);
        log.info("保存店铺数据成功，入参：{}", param);
        return Result.success();
    }

    //查询 属于当前登录用户 所属商家 和满足区域条件的店铺列表
    @Operation(summary = "查询区域下的店铺列表")
    @PostMapping("/admin/basic/store/getStoreByCity")
    public Result<List<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery query) throws BusinessException {
        List<StoreVO> list = storeService.getStoreByCity(query);
        return new Result<>(list);
    }
    //手机小程序定位上报
    @Operation(summary = "手机小程序定位上报")
    @PostMapping("/basic/store/location")
    public Result<Void> location(@RequestBody UserLocationParam param)
        throws BusinessException{
        return Result.success();
    }
    /**
     * 修改店铺
     * @param param
     * @return
     * @throws BusinessException
     */
    @Operation(summary = "修改店铺")
    @PostMapping("/admin/basic/store/update")
    public Result<Void> update(@RequestBody StoreParam param)
            throws BusinessException {
        storeService.update(param);
        return Result.success();
    }
    @Operation(summary = "删除店铺")
    @DeleteMapping("/admin/basic/store/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer storeId)
            throws BusinessException {
        storeService.delete(storeId);
        return Result.success();
    }
    @Operation(summary = "店铺信息")
    @GetMapping("/admin/basic/store/{id}")
    public Result<StoreVO> storeEdit(@PathVariable("id") Integer storeId)
            throws BusinessException {
        StoreVO  vo = storeService.storeEdit(storeId);
        return new Result<>(vo);
    }
    @Operation(summary = "根据ids查询店铺列表")
    @PostMapping("/admin/basic/store/getStoreById")
    public Result<List<StoreVO>> getStoreById(@RequestBody AreaStoreQuery query)
            throws BusinessException {
        List<StoreVO> list = storeService.getStoreById(query);
        return new Result<>(list);
    }

}
